package com.dff.jrfw.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;

import com.dff.jrfw.common.Constant;
import com.dff.jrfw.model.LoginAccountRecordModel;

import java.util.ArrayList;

public class SharedPreferenceUtils {
    private static final String FILE_NAME = "shared_data"; //文件名
    private static volatile SharedPreferenceUtils mInstance;

    private SharedPreferenceUtils() {

    }

    public static SharedPreferenceUtils getInstance() {
        if (mInstance == null) {
            synchronized (SharedPreferenceUtils.class) {
                if (mInstance == null) {
                    mInstance = new SharedPreferenceUtils();
                }
            }
        }
        return mInstance;
    }

    /**
     * 存入键值对
     *
     * @param context
     * @param key
     * @param value
     */

    public void put(Context context, String key, Object value) {
        //判断类型
        String type = value.getClass().getSimpleName();
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) value);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) value);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) value);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) value);
        } else if ("String".equals(type)) {
            editor.putString(key, (String) value);
        }
        editor.apply();
    }

    /**
     * 读取键的值，若无则返回默认值
     *
     * @param context
     * @param key
     * @param defValue 默认值
     * @return
     */
    @Nullable
    public Object get(Context context, String key, Object defValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String type = defValue.getClass().getSimpleName();
        if ("Integer".equals(type)) {
            return sharedPreferences.getInt(key, (Integer) defValue);
        } else if ("Boolean".equals(type)) {
            return sharedPreferences.getBoolean(key, (Boolean) defValue);
        } else if ("Float".equals(type)) {
            return sharedPreferences.getFloat(key, (Float) defValue);
        } else if ("Long".equals(type)) {
            return sharedPreferences.getLong(key, (Long) defValue);
        } else if ("String".equals(type)) {
            return sharedPreferences.getString(key, (String) defValue);
        }
        return null;
    }

    public void clear(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.apply();
    }


    //保存登录账户
    public void saveAccount(Context context, String username) {
        String json = (String) SharedPreferenceUtils.getInstance().get(context, Constant.LOGIN_ACCOUNT_RECORD, "");
        LoginAccountRecordModel loginAccountRecordModel = GsonUtils.gsonToBean(json, LoginAccountRecordModel.class);
        if (loginAccountRecordModel != null && loginAccountRecordModel.getLoginRecord() != null) {
            ArrayList<String> loginRecord = loginAccountRecordModel.getLoginRecord();
            if (!loginRecord.contains(username)) {
                loginRecord.add(0, username);
                loginAccountRecordModel.setLoginRecord(loginRecord);
            } else {
                loginRecord.remove(username);
                loginRecord.add(0, username);
            }
            SharedPreferenceUtils.getInstance().put(context, Constant.LOGIN_ACCOUNT_RECORD, GsonUtils.gsonString(loginAccountRecordModel));
        } else {
            LoginAccountRecordModel accountRecordModel = new LoginAccountRecordModel();
            ArrayList<String> loginRecord = new ArrayList<>();
            loginRecord.add(username);
            accountRecordModel.setLoginRecord(loginRecord);
            SharedPreferenceUtils.getInstance().put(context, Constant.LOGIN_ACCOUNT_RECORD, GsonUtils.gsonString(accountRecordModel));
        }
    }


}
